package com.spring.sunflower.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.makeramen.roundedimageview.RoundedImageView
import com.spring.sunflower.R

/**
 * Created by Kevin on 2021/8/23<br/>
 *
 * Describe:<br/>
 */
class MessageItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var rivCover: RoundedImageView? = null
    private var tvName: TextView? = null
    private var tvTime: TextView? = null
    private var tvLastMsg: TextView? = null
    private var tvMsgCount: TextView? = null

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.layout_message_item_view, this, true)
        rivCover = view.findViewById(R.id.ivCover)
        tvName = view.findViewById(R.id.tvName)
        tvTime = view.findViewById(R.id.tvTime)
        tvLastMsg = view.findViewById(R.id.tvLastMessage)
        tvMsgCount = view.findViewById(R.id.tvMessageCount)
        addView(view)
        invalidate()
    }

    fun setAvatar(str: String) {
        Glide.with(context)
            .load(str)
            .placeholder(R.drawable.ic_placeholder)
            .into(rivCover!!)
        invalidate()
    }

    fun setAvatar(resId: Int) {
        Glide.with(context)
            .load(resId)
            .placeholder(R.drawable.ic_placeholder)
            .into(rivCover!!)
        invalidate()
    }

    fun setName(str: String) {
        tvName!!.text = str
        invalidate()
    }

    fun setTime(str: String) {
        tvTime!!.text = str
        invalidate()
    }

    fun setLastMsg(str: String) {
        tvLastMsg!!.text = str
        invalidate()
    }

    fun setMsgCount(str: String) {
        tvMsgCount!!.text = str
        invalidate()
    }

    fun setMarkRead(b: Boolean) {
        tvMsgCount!!.visibility = if (b) VISIBLE else GONE
        invalidate()
    }

    fun getAvatar(): RoundedImageView = rivCover!!
    fun getName(): TextView = tvName!!
    fun getMsgCount(): TextView = tvMsgCount!!
    fun getTime(): TextView = tvTime!!
    fun getLastMsg(): TextView = tvLastMsg!!
}